﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Management_MedicalDirector_Phongbenh : System.Web.UI.Page
{
    public static string idTotal = "";
    public static Color defautColor;
    bool kt;
    SqlConnection con = new SqlConnection(GlobalConstant.CONNECTION_STRING);
    SqlCommand cmd;
    SqlDataReader reader;
    protected void Page_Load(object sender, EventArgs e)
    {
        //Response.Redirect("~/Management/MedicalDirector/Phongbenh.aspx");
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void phongbenh_click(object sender, EventArgs e)
    {
        ddlIDWard.Enabled = false;
        string id = (sender as LinkButton).CommandArgument;
        idTotal = id;
        //lbTilte2.Text = id;
        
        con.Open();
        string sql = "SELECT * FROM tbRoom WHERE ID='" + id+"'";
        cmd = new SqlCommand(sql, con);
        reader = cmd.ExecuteReader();
        if (reader.Read())
        {
            ddlIDWard.SelectedValue = reader.GetInt32(1).ToString();
            txtRoomname.Text = reader.GetString(2).ToString();
           
        }
        con.Close();
        con.Open();
        sql = "SELECT * FROM tbWard WHERE ID ='"+ddlIDWard.SelectedValue+"'";
        cmd = new SqlCommand(sql,con);
        reader = cmd.ExecuteReader();
        if (reader.Read())
        {
            kt = false;
            txtward.Text = reader.GetString(1);
            txtlocation.Text = reader.GetString(2);
            txtTel.Text = reader.GetString(3);
        }
        con.Close();
        
        update.Enabled = true;
    }
    protected void addnew_click(object sender, EventArgs e)
    {
        //txtid.Text = "";
        ddlIDWard.Enabled = true;  
        update.Enabled = true;
        kt = true;
    }
    protected void update_Click(object sender, EventArgs e)
    {
        string st1 = ddlIDWard.SelectedValue;
        string st2 = txtRoomname.Text;   
        string sql;
        string sql1 = "UPDATE tbRoom SET IDWard='"+st1+"', RoomName='"+st2+"' WHERE ID='"+idTotal+"'";
        string sql2 = "INSERT INTO tbRoom(IDWard,RoomName) VALUES('"+st1+"','"+st2+"') ";
        if (kt) sql = sql2;
        else sql = sql1;
        con.Open();
        cmd = new SqlCommand(sql, con);
        cmd.ExecuteNonQuery();
        con.Close();
        Response.Redirect("~/Management/MedicalDirector/Phongbenh.aspx");
    }
    protected void ddlIDWard_SelectedIndexChanged(object sender, EventArgs e)
    {
        con.Open();
        string sql = "SELECT * FROM tbWard WHERE ID='" + ddlIDWard.SelectedValue + "'";
        cmd = new SqlCommand(sql, con);
        reader = cmd.ExecuteReader();
        if (reader.Read())
        {
            txtward.Text = reader.GetString(1);
            txtlocation.Text = reader.GetString(2);
            txtTel.Text = reader.GetString(3);
        }
        con.Close();
    }
}